from django.test import TestCase

from .models import User
# Create your tests here.

class UserTest(TestCase):

    def test_get_home_renders_template(self):
        """
        url :/home/
        测试home.html
        """
        response = self.client.get('/home/')
        self.assertEqual(response.status_code,200) # 判断是否请求成功
        # self.assertTemplateUsed(response,'home.html') # 判断使用的模板
        self.assertContains(response,'您好')
    
    def test_post_home_add_user_to_db(self):

        data = {
            'username':'wangwu',
            'age':18,
            'active':True
        }
        response = self.client.post('/add/user/',data=data)
        self.assertEqual(response.status_code,302)
        user = User.objects.filter(username='wangwu',age=18).first()
        self.assertEqual(data['active'],user.active)
        if user:
            user.delete()
        
    def test_get_test_exception_info_show(self):

        response = self.client.get('/test/')
        self.assertEqual(response.status_code,500)